                            # 9.1 创建和使用类
# 9.1.1 创建Dog类
class Dog():
    """一次模拟小狗的简单尝试"""

    def __init__(self, name, age):  #类中的函数称为方法
        """初始化属性name和age"""
        self.name = name
        self.age = age

    def sit(self):
        """模拟小狗被命令时蹲下"""
        print(self.name.title() + " is now sitting.")

    def roll_over(self):
        """模拟小狗被命令时打滚"""
        print(self.name.title() + " rolled over!" )
# 9.1.2 根据类创建实例
my_dog = Dog('willie', 6)
# 访问属性
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")
# 调用方法
my_dog.sit()
my_dog.roll_over()
# 再创建一个实例
your_dog = Dog('lucy', 3)
# 访问属性
print("Your dog's name is " + your_dog.name.title() + ".")
print("Your dog is " + str(your_dog.age) + " years old.")
# 调用方法
your_dog.sit()
your_dog.roll_over()
                            # 9.2 使用类和实例
# 9.2.1 Car类
class Car():
    """一次模拟汽车的简单尝试"""

    def __init__(self, make, model, year):
        """初始化汽车的属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0 # 默认初始里程数为0

    def get_descriptive_name(self):
        """返回整洁的描述信息"""
        long_name = str(self.year) + " " + self.make + " " + self.model
        return long_name.title()

    def read_odometer(self):
        """打印一条指出汽车里程的消息"""
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self, mileage):
        """
        将里程表读数设置为指定的值
        禁止将里程表读数往回调
        """
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        """将里程表读数增加指定的量"""
        if miles >= 0:
            self.odometer_reading += miles
        else:
            print("You can't roll back an odometer!")

my_new_car = Car('audi', 'a4', 2016)
print(my_new_car.get_descriptive_name())
# 9.2.2 给属性指定默认值
my_new_car.read_odometer()
# 9.2.3 修改属性的值
my_new_car.update_odometer(23)
my_new_car.read_odometer()

my_new_car.update_odometer(16)
my_new_car.read_odometer()

my_new_car.increment_odometer(100)
my_new_car.read_odometer()

my_new_car.increment_odometer(-100)
my_new_car.read_odometer()
                            # 9.3 继承
# 9.3.1 子类的方法_init_()
class Battery():
    """一次模拟电动汽车电瓶的简单尝试"""

    def __init__(self, battery_size = 70):
        """初始化电瓶的属性"""
        self.battery_size = battery_size

    def describe_battery(self):
        """打印一条描述电瓶容量的消息"""
        print("This car has a " + str(self.battery_size) + "-kwh battery")

    def get_range(self):
        """打印一条消息，指出电瓶的续航里程"""
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270

        msg = "This car can go approximately " + str(range)
        msg += " miles on a full charge."
        print(msg)

    def upgrade_battery(self):
        if self.battery_size != 85:
            self.battery_size = 85

class ElectricCar(Car):
    """
    电动汽车的独特之处
    初始化父类的属性，再初始化电动汽车特有的属性
    """
    
    def __init__(self, make, model, year):
        """初始化父类的属性，再初始化电动汽车特有的属性"""
        super().__init__(make, model, year)
        self.battery = Battery()    # 将一个Battery实例用作ElectricCar类的一个属性

# 实例化
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()

# 9.3.2 Python 2.7中的继承
# 9.3.3 给子类定义属性和方法
# 9.3.4 重写父类的方法
# 9.3.5 将实例用作属性
    # 将大型类拆分成多个协同工作的小类
# 9.3.6 模拟实物

                            # 9.4 导入类
# 9.4.1 导入单个类
from car import Car

my_new_car = Car('audi', 'a4', 2016)
print(my_new_car.get_descriptive_name())

my_new_car.odometer_reading = 23
my_new_car.read_odometer()

# 9.4.2 在一个模块中存储多个类
from car import ElectricCar

my_tesla = ElectricCar('tesla', 'model s', 2016)

print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

# 9.4.3 从一个模块中导入多个类
from car import Car, ElectricCar

my_beetle = Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())

# 9.4.4 导入整个模块
import  car
# 9.4.5 导入模块中的所有类
from car import *
# 9.4.6 在一个模块中导入另一个模块
from car import Car
from electric_car import ElectricCar

my_beetle = Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
                            # 9.5 Python标准库
from collections import OrderedDict # 有序字典

favorite_languages = OrderedDict() # 创建实例

favorite_languages['jen'] = 'python'
favorite_languages['sarah'] = 'c'
favorite_languages['edward'] = 'ruby'
favorite_languages['phil'] = 'python'

for name, language in favorite_languages.items():
    print(name.title() + "'s favorite languages is " + language.title() + ".")
                            # 9.6 类编码风格
                            # 9.7 小结
